package com.gta.edu.sdk.config;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.ezmorph.object.AbstractObjectMorpher;

public class DateMorpher extends AbstractObjectMorpher {
	private String format;

	public DateMorpher(String format) {
		this.format = format;
	}
	
	@Override
	public Object morph(Object obj) {
		if (obj != null) {
			try {
				return new SimpleDateFormat(this.format).parse(obj.toString());
			} catch (ParseException e) {
				return null;
			}
		}
		return null;
	}

	@Override
	public boolean supports(Class clazz) {
		return String.class.isAssignableFrom( clazz );
	}

	@Override
	public Class morphsTo() {
		return Date.class;
	}

}
